
function checkUserName() {
	var username = $("#username").val();
	var tr = $(".wwFormTable > tbody > tr:first");
	if (username === null || $.trim(username).length === 0 ) {
		if (tr.find("td").size() < 3) {
			tr.append("<td><font color='red'>username cannot be null</font></td>");
		}
		$("#username").focus();
		return false;
	} else {
		var td = tr.find("td:last");
		if (td.html() == "<font color=\"red\">username cannot be null</font>") {
			td.remove();
		}
		return true ;
	}
}

function checkPassword() {
	var password = $("#password").val();
	var tr = $(".wwFormTable > tbody > tr:first").next();
	if ( password === null  || $.trim(password).length ===0 ) {
		if ( tr.find("td").size() < 3 ) {
			tr.append("<td><font color='red'>password cannot be null</font></td>");
		}
		$("#password").focus();
		return false ;
	} else {
		var td = tr.find("td:last");
		if ( td.html() == "<font color=\"red\">password cannot be null</font>") {
			td.remove();
		}
		return true;
	}
}

function checkConfirm() {
	var confirm = $("#confirm").val();
	var tr = $(".wwFormTable > tbody > tr:first").next().next();
	if ( confirm === null || $.trim(confirm).length === 0 ) {
		if ( tr.find("td").size() < 3 ) {
			tr.append("<td><font color='red'>confirm password cannot be null</font></td>");
		}
		$("#confirm").focus();
		return false ;
	} else {
		var td = tr.find("td:last");
		if ( td.html() == "<font color=\"red\">confirm password cannot be null</font>") {
			td.remove();
		}
		return true ;
	}
}

function checkPasswordConfirm() {
	if ( checkPassword() && checkConfirm() ) {
		var password = $("#password").val();
		var confirm = $("#confirm").val();
		if ( password != confirm ) {
			$("#password").attr("value","");
			$("#confirm").attr("value","");
			alert("Password and Confirm Password are not match,Please check your password and enter them again");
			return false ;
		}
		return true ;
	}
	return false ;
}

function checkFirstName() {
	var firstname = $("#firstname").val();
	var tr = $(".wwFormTable > tbody > tr:first").next().next().next().next();
	if ( firstname === null || $.trim(firstname).length === 0 ) {
		if ( tr.find("td").size() < 3 ) {
			tr.append("<td><font color='red'>first name cannot be null</font></td>");
		}
		$("#firstname").focus();
		return false ;
	} else {
		var td = tr.find("td:last");
		if ( td.html() == "<font color=\"red\">first name cannot be null</font>"  ) {
			td.remove();
		}
		return true ;
	}
}

function checkLastName() {
	var lastname = $("#lastname").val();
	var tr = $(".wwFormTable > tbody > tr:first").next().next().next();
	if ( lastname === null || $.trim(lastname).length === 0 ) {
		if ( tr.find("td").size() < 3 ) {
			tr.append("<td><font color='red'>last name cannot be null</font></td>");
		}
		$("#lastname").focus();
		return false ;
	} else {
		var td = tr.find("td:last");
		if ( td.html() == "<font color=\"red\">last name cannot be null</font>"  ) {
			td.remove();
		}
		return true ;
	}
}

function checkBirthday() {
	var birthday = $("#birthday").val();
	var tr = $(".wwFormTable > tbody > tr:first").next().next().next().next().next().next();
	if ( birthday != null && $.trim(birthday).length != 0 ) {
		var regx = /^\d{4}\-\d{2}\-\d{2}$/; 
		if (!regx.test(birthday)){
			if(tr.find("td").size()<3){
				tr.append("<td><font color='red'>error fomat</font></td>");
			}
			$("#birthday").focus();
			return false;
		}
	}
	var td=tr.find("td:last");
	if(td.html()=="<font color=\"red\">error fomat</font>")	{
		td.remove();
	}
	return true;
}

function checkCellPhone() {
	var cellPhone = $("#cellphone").val();
	var tr = $(".wwFormTable > tbody > tr:first").next().next().next().next().next().next().next();
	if ( cellPhone != null && $.trim(cellPhone).length != 0 ) {	
		var regx = /^(\+\d{2,3}\-)?\d{11}$/;
		if (! regx.test(cellPhone)) {
			if ( tr.find("td").size() < 3 ) {
				tr.append("<td><font color='red'>your cellphone number is not correct</font></td>");
			}
			$("#cellphone").focus();
			return false ;
		} 
	}
	var td = tr.find("td:last");
	if (td.html() == "<font color=\"red\">your cellphone number is not correct</font>" ) {
		td.remove();
	}	
	return true ;
}

function checkOfficePhone() {
	var officeTel = $("#officeTel").val();
	var tr = $(".wwFormTable > tbody > tr:first").next().next().next().next().next().next().next().next();
	if ( officeTel != null && $.trim(officeTel).length != 0 ) {
		var regx = /^(\d{3,4}\-)?\d{7,8}$/;
		if ( ! regx.test(officeTel) ) {
			if ( tr.find("td").size() < 3 ) {
				tr.append("<td><font color='red'>Your office phone number is not correct</font></td>");
			}
			$("#officeTel").focus();
			return false ;
		}
	}
	var td = tr.find("td:last");
	if ( td.html() == "<font color=\"red\">Your office phone number is not correct</font>" ) {
		td.remove();
	}
	return true ;
}

function checkFamilyPhone() {
	var familyTel = $("#familyTel").val();
	var tr = $(".wwFormTable > tbody > tr:first").next().next().next().next().next().next().next().next().next();
	if ( familyTel != null && $.trim(familyTel).length != 0 ) {
		var regx = /^(\d{3,4}\-)?\d{7,8}$/;
		if ( ! regx.test(familyTel) ) {
			if ( tr.find("td").size() < 3 ) {
				tr.append("<td><font color='red'>Your family phone number is not correct</font></td>");
			}
			$("#familyTel").focus();
			return false ;
		}
	}
	var td = tr.find("td:last");
	if ( td.html() == "<font color=\"red\">Your family phone number is not correct</font>" ) {
		td.remove();
	}
	return true ;
}

function checkHospitalPhone() {
	var hospitalTel = $("#hospitalTel").val();
	var tr = $(".wwFormTable > tbody > tr:first").next().next().next().next().next().next().next().next().next().next();
	if( hospitalTel != null && $.trim(hospitalTel).length != 0) {
		var regx = /^(\d{3,4}\-)?\d{7,8}$/;
		if (! regx.test(hospitalTel)){
			if(tr.find("td").size() <3 ){
				tr.append("<td><font color='red'>your hospital phone number is not correct</font></td>");
			}	
			$("#hospitalTel").focus();
			return false;
		}
	}
	var td=tr.find("td:last");
	if(td.html=="<font color=\"red\">your hospital phone number is not correct</font>"){
		td.remove();
	}
	return true;	
} 

function checkEmail() {
	var email = $("#email").val();
	var flag = true ;
	var tr = $(".wwFormTable > tbody > tr:last").prev().prev().prev().prev().prev().prev();
	if ( email != null && $.trim(email).length != 0 ) {
		var regx = /^\w{3,}@\w+(\.\w+)+$/;
		if ( ! regx.test(email) ) {
			if ( tr.find("td").size() < 3 ) {
				tr.append("<td><font color='red'>Your email address is not correct</font></td>");
			}
			$("#email").focus();
			return false;
		} 
	}
	var td = tr.find("td:last");
	if ( td.html() == "<font color=\"red\">Your email address is not correct</font>" ) {
		td.remove();
	}
	return true ;
}

function checkFax() {
	var fax = $("#fax").val();
	var tr = $(".wwFormTable > tbody > tr:last").prev().prev().prev().prev().prev();
	if( fax != null && $.trim(fax).length != 0) {
		var regx = /^(\d{3,4}\-)?\d{7,8}$/;
		if (! regx.test(fax)){
			if(tr.find("td").size() <3 ){
				tr.append("<td><font color='red'>your fax number is not correct</font></td>");
			}	
			$("#fax").focus();
			return false;
		}
	}
	var td=tr.find("td:last");
	if(td.html=="<font color=\"red\">your fax number is not correct</font>"){
		td.remove();
	}
	return true;	
}

function checkForm() {
	if (checkUserName() && checkPasswordConfirm() && checkFirstName() && checkLastName() && 
			checkBirthday() && checkCellPhone() && checkOfficePhone() && checkFamilyPhone() && 
			checkHospitalPhone() && checkEmail() && checkFax()) {
		return true;
	}
	return false;
}

